Imports System.Data.SqlClient

Public Class Barrio : Implements IBusquedaFiltrado

    Private idBarrio As Integer
    Private descri As String
    Private loc As Localidad

    Public Property Identificado() As Integer
        Get
            Return idBarrio
        End Get
        Set(ByVal value As Integer)
            idBarrio = value
        End Set
    End Property

    Public Property Descripcion() As String
        Get
            Return descri
        End Get
        Set(ByVal value As String)
            descri = value
        End Set
    End Property

    Public Property Localidad() As Localidad
        Get
            Return loc
        End Get
        Set(ByVal value As Localidad)
            loc = value
        End Set
    End Property
    Public Sub New()
        idBarrio = Nothing
        descri = ""
        loc = Nothing
    End Sub

    Public Function Buscar(ByVal id As Integer, ByRef con As ConexionBD) As Boolean _
                    Implements IBusquedaFiltrado.Buscar
        Dim comando As SqlDataAdapter
        Dim rsBarrio As DataTableReader
        Dim dato As DataTable
        Dim sql As String
        Dim lResp As Boolean = False

        sql = String.Format("SELECT * FROM BARRIO WHERE idbarrio ={0} ", id)
        comando = New SqlDataAdapter(sql, con.ConexionBase)
        dato = New DataTable("estados")
        comando.Fill(dato)
        rsBarrio = dato.CreateDataReader()
        comando.Dispose()
        dato.Dispose()

        If rsBarrio.Read Then
            idBarrio = System.Convert.ToInt16(rsBarrio("idBarrio"))
            Me.descri = rsBarrio("descrip").ToString
            loc = New Localidad()
            loc.Buscar(rsBarrio("idLoc"), con)
            lResp = True
        Else
            lResp = False
            MsgBox("El Barrio no existe")
        End If
        rsBarrio.Close()
        Return lResp
    End Function

    Public Function Agregar(ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Agregar
        Dim sql As String = ""
        Dim comando As SqlCommand = Nothing

        sql = "INSERT INTO BARRIO (idBarrio, descrip, idLocalidad)" + _
              "  VALUES({0},'{1}',{2})"
        sql = String.Format(sql, Me.idBarrio, Me.descri, loc.Identificador)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If

    End Function

    Public Function Modificar(ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Modificar
        Dim sql As String = ""
        Dim comando As SqlCommand = Nothing

        sql = "UPDATE BARRIO " + _
              "  SET DESCRIP = '{0}'," + _
              "      IDLOCALIDAD ={2}" + _
              "  WHERE IDBARRIO = {3}"
        sql = String.Format(sql, Me.descri, Me.loc.Identificador, Me.idBarrio)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function Eliminar(ByVal id As Integer, ByRef con As ConexionBD) As Boolean _
            Implements IBusquedaFiltrado.Eliminar

        Dim sql As String = ""
        Dim comando As SqlCommand = Nothing

        sql = "DELETE FROM BARRIO WHERE IDBARRIO ={0}"
        sql = String.Format(sql, id)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If

    End Function
    Public Function BuscarDatos(ByRef con As ConexionBD) As DataSet

        Dim sql As String = ""
        Dim adapter As SqlDataAdapter = Nothing
        Dim datos As DataSet = New DataSet()

        sql = "SELECT * FROM BARRIO"
        adapter = New SqlDataAdapter(sql, con.ConexionBase)
        adapter.Fill(datos, "barrio")

        Return datos
    End Function
    Public Function BuscarDatosxLoc(ByVal idLoc As Integer, ByRef con As ConexionBD) As DataSet

        Dim sql As String = ""
        Dim adapter As SqlDataAdapter = Nothing
        Dim datos As DataSet = New DataSet()

        sql = String.Format("SELECT * FROM BARRIO WHERE IdLoc={0}", idLoc)
        adapter = New SqlDataAdapter(sql, con.ConexionBase)
        adapter.Fill(datos, "barrio")

        Return datos
    End Function
    Public Function BuscarId(ByVal DescBar As String, ByRef con As ConexionBD) As Boolean

        Dim comando As SqlCommand
        Dim rs As SqlDataReader
        Dim sql As String
        Dim lResp As Boolean = False

        sql = String.Format("SELECT * FROM BARRIO WHERE Descrip='{0}'", DescBar)
        comando = New SqlCommand(sql, con.ConexionBase)
        rs = comando.ExecuteReader()
        comando.Dispose()


        If rs.Read Then
            idBarrio = System.Convert.ToInt16(rs("idBarrio"))
            descri = rs("Descrip").ToString
            lResp = True
        Else
            lResp = False
        End If
        rs.Close()
        Return lResp

    End Function
End Class
